package com.ny.service.Impl;

import com.ny.entity.Device;
import com.ny.mapper.DeviceMapper;
import com.ny.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;


    @Override
    public List<Device> findAll() {
        return deviceMapper.findAll();
    }

    @Override
    public List<Device> findByDeviceName(String deviceName) {
        // 先尝试方法名解析
        List<Device> result = deviceMapper.findByDeviceName(deviceName);

        // 如果方法名解析不工作，使用自定义查询
        if (result.isEmpty()) {
            result = deviceMapper.findByDeviceNameCustom(deviceName);
        }

        // 如果还是空，尝试正则表达式查询
        if (result.isEmpty()) {
            result = deviceMapper.findByDeviceNameRegex(deviceName);
        }

        return result;
    }

    @Override
    public List<Device> findByDeviceNameContaining(String keyword) {
        // 使用正则表达式实现模糊查询
        return deviceMapper.findByDeviceNameRegex(".*" + keyword + ".*");
    }
    @Override
    public List<Device> findByStatus(Integer status) {
        return deviceMapper.findByStatus(status);
    }

    @Override
    public List<Device> findByTimeRangeAndStatus(Date startTime, Date endTime, Integer status) {
        return deviceMapper.findByCreatedTimeBetweenAndStatus(startTime, endTime, status);
    }


}